<html>
<head>
<meta charset="utf-8">
<title>file</title>
<script type="text/javascript" src="src/Global.js"></script>
<script type="text/javascript" src="src/object.js"></script>
<script type="text/javascript" src="src/Util.js"></script>
<script type="text/javascript" src="src/BinData.js"></script>
<script type="text/javascript" src="src/FilSys.js"></script>
<script type="text/javascript" src="src/Inode.js"></script>
<script type="text/javascript" src="src/Directory.js"></script>
<script type="text/javascript" src="src/DirectoryEntry.js"></script>
<script type="text/javascript" src="src/File.js"></script>
<script type="text/javascript" src="src/ExeHeader.js"></script>
<script type="text/javascript" src="src/OpType.js"></script>
<script type="text/javascript" src="src/OpHandler.js"></script>
<script type="text/javascript" src="src/OpCode.js"></script>
<script type="text/javascript" src="src/SystemCall.js"></script>
<script type="text/javascript" src="src/Disassembler.js"></script>
<script type="text/javascript" src="src/Exe.js"></script>
<script type="text/javascript" src="src/PS.js"></script>
<script type="text/javascript" src="src/Register.js"></script>
<script type="text/javascript" src="src/Processor.js"></script>
<script type="text/javascript" src="src/PDP11.js"></script>
<script type="text/javascript" src="src/ProcFile.js"></script>
<script type="text/javascript" src="src/Proc.js"></script>
<script type="text/javascript" src="src/Kernel.js"></script>
<script type="text/javascript" src="src/Trace.js"></script>
<script type="text/javascript">



window.addEventListener( 'dragover', function( event ) {
  event.preventDefault( ) ;
}, false ) ;

window.addEventListener( 'drop', function( event ) {

  event.preventDefault( ) ;
  var file = event.dataTransfer.files[ 0 ] ;

  var reader = new FileReader( ) ;
  reader.onload = function( event ) {

    bin_data = object( BinData ).create( event.target.result ) ;
    pdp11 = object( PDP11 ).create( ) ;

    superblock = object( Filsys ).create( bin_data ) ;
    superblockview.value = superblock.string( ) ;

    var ul = document.createElement( 'ul' ) ;
    var li = document.createElement( 'li' ) ;
    var a  = document.createElement( 'a' ) ;
    a.href = "javascript:void( 0 )" ;
    a.setAttribute( "onclick", "viewFile( 1, '/' ); foldDirectory( this.parentElement, 1 )" ) ;
    a.innerHTML = "/" ;

    li.appendChild( a ) ;
    ul.appendChild( li ) ;
    resultview.appendChild( ul ) ;

    current_dir_i_number = 1 ;

    var inode = get_inode( 0x1d6 ) ;
    var file = object( File ).create( inode ) ;
    var exe = object( Exe ).create( file ) ;
/*
    window.alert( file.inode.data_addr( 0 ).toString( 16 ) ) ;
    for( var i = 0; i < 0x50; i += 2 ) {
      window.alert( i.toString( 16 ) + ':' + file.word_data( i ).toString( 16 ) ) ;
    }
*/
  } ;
  reader.onerror = function( e ) {
    resultview.innerHTML += 'Error!<br>' ;
    resultview.innerHTML += '<br>' ;
    for( var key in reader.error ) {
      resultview.innerHTML += key + '=' + reader.error[ key ] + '<br>' ;
    }
  };
  reader.readAsArrayBuffer( file ) ;

}, false ) ;

function viewFile( i_number, name ) {

  var inode = get_inode( i_number ) ;
  var file = object( File ).create( inode ) ;

  if( file.isExe( ) ) {
    var exe = object( Exe ).create( file ) ;
    textview.value = exe.disassemble( ) ;
  } else {
    var e = ( inode.is_IFDIR( ) && ! inode.is_IFCHR( ) )
              ? object( Directory ).create( inode )
              : object( File ).create( inode ) ;
    textview.value = e.string( ) ;
  }

  inodeview.value = inode.string( ) ;
  binaryview.value = file.binary_data( ) ;
  runview.value = '' ;

  current_i_number = i_number ;
  current_name = name ;

}

function foldDirectory( parent, i_number ) {

  var ele = parent.firstChild.nextSibling ;

  if( ele ) {

    while( ele ) {
      parent.removeChild( ele ) ;
      ele = ele.nextSibling ;
    }

  } else {

    var inode = get_inode( i_number ) ;

    if( ! inode.is_IFDIR( ) || inode.is_IFCHR( ) )
      return ;

    current_dir_i_number = i_number ;
    var dir = object( Directory ).create( inode ) ;
    var ul = document.createElement( 'ul' ) ;
    var has_children = false ;

    for( var i = 0; i < dir.size( ); i++ ) {

      var e = dir.entry( i ) ;

      if( e.empty( ) || e.is_relative( ) )
        continue ;

      var li = document.createElement( 'li' ) ;
      var a  = document.createElement( 'a' ) ;
      a.href = "javascript:void( 0 )" ;
      a.setAttribute( "onclick",
                      "viewFile( " + e.i_number + ", '" + e.name + "' ); foldDirectory( this.parentElement, " + e.i_number + " )" ) ;
      a.innerHTML = e.get_displayed_name( ) ;

      li.appendChild( a ) ;
      ul.appendChild( li ) ;
      has_children = true ;

    }

    if( has_children )
      parent.appendChild( ul ) ;

  }

}

function run( ) {

  while( traceview.firstChild )
    traceview.removeChild( traceview.firstChild ) ;

  var inode = get_inode( current_i_number ) ;
  var file = object( File ).create( inode ) ;

  if( ! file.isExe( ) )
    return ;

  runview.value = '' ;
  trace = object( Trace ).create( ) ;
  var exe = object( Exe ).create( file ) ;
  var proc = object( Proc ).create( ) ;
  proc.load( exe ) ;
  try {
    proc.run( ) ;
  } catch( e ) {
    console.log( e ) ;
  }
  var link = document.createElement( 'a' ) ;
  link.href = trace.getLink( ) ;
  link.innerHTML = 'trace data.' ;
  traceview.appendChild( link ) ;

}


function init( ) {

  resultview     = document.getElementById( "resultview" ) ;
  textview       = document.getElementById( "textview" ) ;
  binaryview     = document.getElementById( "binaryview" ) ;
  inodeview      = document.getElementById( "inodeview" ) ;
  superblockview = document.getElementById( "superblockview" ) ;
  runview        = document.getElementById( "runview" ) ;
  argview        = document.getElementById( "argview" ) ;
  traceview      = document.getElementById( "traceview" ) ;

}

</script>
</head>
<body onLoad="init( )">
<table>
<tr>
<td valign="top">
<p>
<div id="resultview"></div>
</p>
</td>
<td valign="top">
<p>
<textarea id="superblockview" cols="100" rows="20">
</textarea>
</p>
<p>
<textarea id="inodeview" cols="100" rows="20">
</textarea>
</p>
<p>
<textarea id="textview" cols="100" rows="30">
</textarea>
</p>
<p>
<textarea id="binaryview" cols="100" rows="30">
</textarea>
</p>
<p>
<button onClick="run( )">run</button>
<input type="text" id="argview" />
<span id="traceview"></span>
<br />
<textarea id="runview" cols="100" rows="20">
</textarea>
</p>
</td>
</tr>
</table>
</body>
</html> 